#!/bin/bash
# 作者：无涯有涯
# 功能：配置 kubeconfig 文件，将 /etc/kubernetes/admin.conf 复制到 $HOME/.kube/config 并设置权限
# 用法：curl -sSL https://gitee.com/veryx/galaxy-tools/raw/master/1-k8s/k8s_kubeconfig.sh | bash

if [ -f $HOME/.kube/config ] && [ "$1" != "--force" ]; then
    echo "当前用户的 kubeconfig 配置文件已存在，无需重复配置。"
    exit 0
else
    mkdir -p $HOME/.kube
    sudo cp -n /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config

    sudo sed -i '\|^export KUBECONFIG=/etc/kubernetes/admin\.conf$|d' /etc/profile

    if [ -n "$KUBECONFIG" ] && [ "$KUBECONFIG" = "/etc/kubernetes/admin.conf" ]; then
        unset KUBECONFIG
    fi
fi